package com.wude.guild.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.UserInfo;
import com.wude.guild.MineManagerActivity;
import com.wude.guild.R;
import com.wude.guild.servercore.mints.TitleBaseFragment;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/11/23 0023.
 */

public class ModifySexFragment extends TitleBaseFragment implements View.OnClickListener {
    private Unbinder unbinder;
    @BindView(R.id.check_boy)
    CheckBox checkBoy;
    @BindView(R.id.check_girl)
    CheckBox checkGirl;
    private UserInfo loginUser;

    private MineManagerActivity activity;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_modifysex, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("ModifySexFragment");
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("ModifySexFragment");
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);

        setHeaderTitle("修改性别");
        TextView rightTextView = getTitleHeaderBar().getRightTextView();
        rightTextView.setText("保存");
        rightTextView.setOnClickListener(this);

        loginUser = Utils.getLoginUser();
        if (loginUser.sex == 0) {
            checkBoy.setChecked(true);
            checkGirl.setChecked(false);
        } else {
            checkBoy.setChecked(false);
            checkGirl.setChecked(true);
        }

        checkBoy.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    checkGirl.setChecked(false);
                }
            }
        });
        checkGirl.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    checkBoy.setChecked(false);
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onClick(View view) {
        HashMap<String, String> map = new HashMap<>();
        map.put("token", loginUser.token);
        if (checkGirl.isChecked()) {
            map.put("sex", "1");        //女
        } else if (checkBoy.isChecked()) {
            map.put("sex", "0");          //男
        } else {
            ToastUtil.showToast("请选择性别");
            return;
        }
        Log.e("请求参数", map.toString());
        HttpCom.POST(handler, HttpCom.getUserURL(), map, false);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = (MineManagerActivity) activity;
    }

    /**
     * 修改性别
     */
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    UserInfo userInfo = HttpUtils.DNSUser(msg.obj.toString());
                    if (userInfo != null) {
                        activity.popTopFragment(null);
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络异常");
                    break;
            }
        }
    };
}
